package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Fwpz;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lenovo
 * Date: 13-5-21
 * Time: 下午2:25
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class FwpzDao extends SimpleHibernateTemplate<Fwpz, Long> {

    public Page<Fwpz> findFwpzList(String name, String action, int pageNo, int pageSize) {
        Page<Fwpz> page = new Page<Fwpz>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("name");
        if (StringUtils.isNotEmpty(name)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("name", name, MatchMode.START)});
        } else if (StringUtils.isNotEmpty(action)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("action", action, MatchMode.START)});
        } else
            return this.findAll(page);
    }

    public Boolean checkUnique(Long id, Object propertyValue, String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Fwpz  where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Fwpz  where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue, id);
        }
    }


    public List<Fwpz> findByAction(String action) {
        return this.find("from Fwpz where action = ?",action);
    }
}
